package com.a214.yxz.yuejian013.Model;


import com.a214.yxz.yuejian013.JavaBean.AboutUsBean;
import com.a214.yxz.yuejian013.JavaBean.ClubBean;
import com.a214.yxz.yuejian013.JavaBean.ClubcourseBean;
import com.a214.yxz.yuejian013.JavaBean.ClubvideoBean;
import com.a214.yxz.yuejian013.JavaBean.ColBean;
import com.a214.yxz.yuejian013.JavaBean.FollowBean;
import com.a214.yxz.yuejian013.JavaBean.MasterBean;
import com.a214.yxz.yuejian013.JavaBean.OrderBean;
import com.a214.yxz.yuejian013.JavaBean.ShaireplyBean;
import com.a214.yxz.yuejian013.JavaBean.ShaituBean;
import com.a214.yxz.yuejian013.JavaBean.UserBean;
import com.a214.yxz.yuejian013.Listener.ServiceListener;
import com.a214.yxz.yuejian013.Service.RegisterService;
import com.a214.yxz.yuejian013.Service.ServiceIface;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;


/**
 * Created by Oliver on 9/29/2017.
 */

public class ResModel<T> implements ServiceIface {
    private Retrofit retrofit;
    private RegisterService service;
    private static final String BASE_URL = "http://172.24.10.175/workout/api.php/";
    private String SESSION_ID;

    public ResModel(String type, String sessionID) {
        if (type.equals("gson")) {
            retrofit = new Retrofit.Builder()
                    .baseUrl(BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
        } else {
            retrofit = new Retrofit.
                    Builder().
                    baseUrl(BASE_URL).//ScalarsConverterFactory

                    addConverterFactory(GsonConverterFactory.create()).

                    build();
        }
        service = retrofit.create(RegisterService.class);
        SESSION_ID = sessionID;
    }

    @Override
    public void getList(String mod, String userid, int page, String listorder, final ServiceListener listener) {
        switch (mod) {
            case "club":
                Call<List<ClubBean>> callClub = service.getClubList(mod, page, listorder, SESSION_ID);
                callClub.enqueue(new Callback<List<ClubBean>>() {
                    @Override
                    public void onResponse(Call<List<ClubBean>> call, Response<List<ClubBean>> response) {
                        listener.onResponse(response.body());
                    }

                    @Override
                    public void onFailure(Call<List<ClubBean>> call, Throwable t) {
                        listener.onFail(t.toString());
                    }
                });
                break;
            case "fans":
                Call<List<FollowBean>> callFollowList = service.getFollowList(mod, userid, listorder, page, SESSION_ID);
                callFollowList.enqueue(new Callback<List<FollowBean>>() {
                    @Override
                    public void onResponse(Call<List<FollowBean>> call, Response<List<FollowBean>> response) {
                        listener.onResponse(response.body());
                    }

                    @Override
                    public void onFailure(Call<List<FollowBean>> call, Throwable t) {
                        listener.onFail(t.toString());
                    }
                });
                break;
            case "shaitu":
                Call<List<ShaituBean>> callShaituList = service.getShaituList_user(mod, userid, "1", page, SESSION_ID);
                callShaituList.enqueue(new Callback<List<ShaituBean>>() {
                    @Override
                    public void onResponse(Call<List<ShaituBean>> call, Response<List<ShaituBean>> response) {
                        listener.onResponse(response.body());
                    }

                    @Override
                    public void onFailure(Call<List<ShaituBean>> call, Throwable t) {
                        listener.onFail(t.toString());
                    }
                });
                break;
        }
    }

    @Override
    public void getOne(String mod, String id, final ServiceListener listener) {
        switch (mod) {
            case "club":
                Call<ClubBean> callClub = service.getClubDetail(mod, id, SESSION_ID);
                callClub.enqueue(new Callback<ClubBean>() {
                    @Override
                    public void onResponse(Call<ClubBean> call, Response<ClubBean> response) {
                        listener.onResponse(response.body());
                    }

                    @Override
                    public void onFailure(Call<ClubBean> call, Throwable t) {
                        listener.onFail(t.toString());
                    }
                });
                break;

        }
    }

    @Override
    public void getClubDetail(String mod, String club_id, final ServiceListener listener) {
        switch (mod) {
            case "master":
                Call<List<MasterBean>> callmaster = service.getClubMaster(mod, club_id, SESSION_ID);
                callmaster.enqueue(new Callback<List<MasterBean>>() {
                    @Override
                    public void onResponse(Call<List<MasterBean>> call, Response<List<MasterBean>> response) {
                        listener.onResponse(response.body());
                    }

                    @Override
                    public void onFailure(Call<List<MasterBean>> call, Throwable t) {
                        listener.onFail(t.toString());
                    }
                });
                break;
            case "clubvideo":
                Call<List<ClubvideoBean>> callvideo = service.getClubVideo(mod, club_id, SESSION_ID);
                callvideo.enqueue(new Callback<List<ClubvideoBean>>() {
                    @Override
                    public void onResponse(Call<List<ClubvideoBean>> call, Response<List<ClubvideoBean>> response) {
                        listener.onResponse(response.body());
                    }

                    @Override
                    public void onFailure(Call<List<ClubvideoBean>> call, Throwable t) {
                        listener.onFail(t.toString());
                    }
                });
                break;
            case "course":
                Call<List<ClubcourseBean>> callcourse = service.getClubCourse(mod, club_id, SESSION_ID);
                callcourse.enqueue(new Callback<List<ClubcourseBean>>() {
                    @Override
                    public void onResponse(Call<List<ClubcourseBean>> call, Response<List<ClubcourseBean>> response) {
                        listener.onResponse(response.body());
                    }

                    @Override
                    public void onFailure(Call<List<ClubcourseBean>> call, Throwable t) {
                        listener.onFail(t.toString());
                    }
                });
                break;
        }
    }

    @Override
    public void getCollectionResult(String fan_id, String userid, String type, final ServiceListener listener) {
        Call<String> callColRes = service.getCollectionResult("fans", fan_id, userid, type, SESSION_ID);
        callColRes.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                listener.onResponse(response.body());
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                listener.onFail(t.toString());
            }
        });
    }

    @Override
    public void collect(String fan_id, String userid, String type, final ServiceListener listener) {
        Call<String> callCollect = service.collect(fan_id, userid, type, SESSION_ID);
        callCollect.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                listener.onResponse(response.body());
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                listener.onFail(t.toString());
            }
        });
    }

    @Override
    public void uncollect(String fan_id, String userid, String type, final ServiceListener listener) {
        Call<String> callUnCollect = service.uncollect("fans", fan_id, userid, type, SESSION_ID);
        callUnCollect.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                listener.onResponse(response.body());
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                listener.onFail(t.toString());
            }
        });
    }

    @Override
    public void getColList(String userid, String type, final ServiceListener listener) {
        Call<List<ColBean>> callColList = service.getColList("fans", userid, type, SESSION_ID);
        callColList.enqueue(new Callback<List<ColBean>>() {
            @Override
            public void onResponse(Call<List<ColBean>> call, Response<List<ColBean>> response) {
                listener.onResponse(response.body());
            }

            @Override
            public void onFailure(Call<List<ColBean>> call, Throwable t) {
                listener.onFail(t.toString());
            }
        });
    }

    @Override
    public void getShaituList(String club_id, int page, final ServiceListener listener) {
        if (club_id.equals("")) {
            Call<List<ShaituBean>> callShaituList = service.getShaituList("shaitu", SESSION_ID, page, "1");
            callShaituList.enqueue(new Callback<List<ShaituBean>>() {
                @Override
                public void onResponse(Call<List<ShaituBean>> call, Response<List<ShaituBean>> response) {
                    listener.onResponse(response.body());
                }

                @Override
                public void onFailure(Call<List<ShaituBean>> call, Throwable t) {
                    listener.onFail(t.toString());
                }
            });
        } else {
            Call<List<ShaituBean>> callShaituList = service.getShaituListByClub("shaitu", SESSION_ID, page, "1", club_id);
            callShaituList.enqueue(new Callback<List<ShaituBean>>() {
                @Override
                public void onResponse(Call<List<ShaituBean>> call, Response<List<ShaituBean>> response) {
                    listener.onResponse(response.body());
                }

                @Override
                public void onFailure(Call<List<ShaituBean>> call, Throwable t) {
                    listener.onFail(t.toString());
                }
            });
        }

    }

    @Override
    public void getShaituDetail(String shaitu_id, final ServiceListener listener) {
        Call<ShaituBean> callShaituDetail = service.getShaituDetail("shaitu", shaitu_id, "1", SESSION_ID);
        callShaituDetail.enqueue(new Callback<ShaituBean>() {
            @Override
            public void onResponse(Call<ShaituBean> call, Response<ShaituBean> response) {
                listener.onResponse(response.body());
            }

            @Override
            public void onFailure(Call<ShaituBean> call, Throwable t) {
                listener.onFail(t.toString());
            }
        });
    }

    @Override
    public void getShaireply(String shaitu_id, final ServiceListener listener) {
        Call<List<ShaireplyBean>> callShaituComment = service.getShaireply("shaireply", shaitu_id, "1", SESSION_ID);
        callShaituComment.enqueue(new Callback<List<ShaireplyBean>>() {
            @Override
            public void onResponse(Call<List<ShaireplyBean>> call, Response<List<ShaireplyBean>> response) {
                listener.onResponse(response.body());
            }

            @Override
            public void onFailure(Call<List<ShaireplyBean>> call, Throwable t) {
                listener.onFail(t.toString());
            }
        });
    }

    @Override
    public void createReply(String shaitu_id, String userid, String reply_userid, String reply, final ServiceListener listener) {
        Call<String> callCreateReply = service.createReply("shaireply", shaitu_id, userid, reply_userid, reply);
        callCreateReply.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                listener.onResponse(response.body());
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                listener.onFail(t.toString());
            }
        });
    }

    @Override
    public void getMine(String userid, final ServiceListener listener) {
        Call<UserBean> callUser = service.getmine(userid, "rca5rlvkb9ap8mtkf25udtbe85");
        callUser.enqueue(new Callback<UserBean>() {
            @Override
            public void onResponse(Call<UserBean> call, Response<UserBean> response) {
                listener.onResponse(response.body());
            }

            @Override
            public void onFailure(Call<UserBean> call, Throwable t) {
                listener.onFail(t.toString());
            }
        });
    }

    @Override
    public void aboutUs(final ServiceListener listener) {
        Call<AboutUsBean> callUs = service.aboutUs(SESSION_ID);
        callUs.enqueue(new Callback<AboutUsBean>() {
            @Override
            public void onResponse(Call<AboutUsBean> call, Response<AboutUsBean> response) {
                listener.onResponse(response.body());
            }

            @Override
            public void onFailure(Call<AboutUsBean> call, Throwable t) {
                listener.onFail(t.toString());
            }
        });
    }

    @Override
    public void getAllOrder(String userid, int page, final ServiceListener listener) {
        Call<List<OrderBean>> callAllOrder = service.getAllOrder("sign", userid, page, SESSION_ID);
        callAllOrder.enqueue(new Callback<List<OrderBean>>() {
            @Override
            public void onResponse(Call<List<OrderBean>> call, Response<List<OrderBean>> response) {
                listener.onResponse(response.body());
            }

            @Override
            public void onFailure(Call<List<OrderBean>> call, Throwable t) {
                listener.onFail(t.toString());
            }
        });
    }

    @Override
    public void getOrderByState(String userid, int page, String vstate, final ServiceListener listener) {
        Call<List<OrderBean>> callOrder = service.getOrderByState("sign", userid, vstate, page, SESSION_ID);
        callOrder.enqueue(new Callback<List<OrderBean>>() {
            @Override
            public void onResponse(Call<List<OrderBean>> call, Response<List<OrderBean>> response) {
                listener.onResponse(response.body());
            }

            @Override
            public void onFailure(Call<List<OrderBean>> call, Throwable t) {
                listener.onFail(t.toString());
            }
        });
    }

    @Override
    public void updateUserName(String userid, String user_name, final ServiceListener listener) {
        Call<String> callUpdateName = service.updateUserName(userid, user_name, SESSION_ID);
        callUpdateName.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                listener.onResponse(response.body());
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                listener.onFail(t.toString());
            }
        });
    }

    @Override
    public void refund(String id, String type, final ServiceListener listener) {
        Call<String> callRefund = service.updateUserPass(id, type, SESSION_ID);
        callRefund.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                listener.onResponse(response.body());
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                listener.onFail(t.toString());
            }
        });
    }

    @Override
    public void updateUserPass(String userid, String password, final ServiceListener listener) {
        Call<String> callUpdatePass = service.updateUserPass(userid, password, SESSION_ID);
        callUpdatePass.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                listener.onResponse(response.body());
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                listener.onFail(t.toString());
            }
        });
    }
}
